#!/bin/bash
###############################################################################
#       脚本名称: test_utils_character.sh
#       功能描述: 字符串处理工具(utils_character.sh)单元测试
#       作者: 林再来
#       日期: 2025-10-20
#       版本: 1.0
#       功能:
#           1、测试字符串处理工具的所有核心功能
#           2、验证编码转换、字符类型检测、字符串格式化等功能
#           3、确保字符串处理工具的独立性和正确性
###############################################################################

# 切换到脚本目录
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1

# 加载测试框架
source ../test_framework.sh

# 加载被测模块
source ../../../scripts/utils/utils_character.sh

# 创建临时测试目录
TEST_DIR=$(create_test_dir)

###############################################################################
# 测试用例
###############################################################################

# 测试: 字符类型检测
test_utils_character_get_type() {
    local result=$(utils_character_get_type "a")
    assert_equals "letter" "$result" "字母应该被识别为letter类型"
    
    local result=$(utils_character_get_type "1")
    assert_equals "digit" "$result" "数字应该被识别为digit类型"
    
    local result=$(utils_character_get_type "中")
    assert_equals "chinese" "$result" "中文字符应该被识别为chinese类型"
    
    local result=$(utils_character_get_type " ")
    assert_equals "space" "$result" "空格应该被识别为space类型"
    
    local result=$(utils_character_get_type "@")
    assert_equals "symbol" "$result" "符号应该被识别为symbol类型"
}

# 测试: 字符类型判断函数
test_utils_character_type_checks() {
    assert_true "字母检查应该正确" "utils_character_is_letter 'a'"
    assert_false "数字不应该被识别为字母" "utils_character_is_letter '1'"
    
    assert_true "数字检查应该正确" "utils_character_is_digit '1'"
    assert_false "字母不应该被识别为数字" "utils_character_is_digit 'a'"
    
    assert_true "中文字符检查应该正确" "utils_character_is_chinese '中'"
    assert_false "英文字母不应该被识别为中文" "utils_character_is_chinese 'a'"
    
    assert_true "字母数字检查应该正确" "utils_character_is_alphanumeric 'a'"
    assert_true "字母数字检查应该正确" "utils_character_is_alphanumeric '1'"
    assert_false "符号不应该被识别为字母数字" "utils_character_is_alphanumeric '@'"
}

# 测试: 字符串格式化函数
test_utils_character_formatting() {
    local result=$(utils_character_substring "hello world" 0 5)
    assert_equals "hello" "$result" "字符串截取应该正确"
    
    local result=$(utils_character_substring "hello world" 6)
    assert_equals "world" "$result" "字符串截取到末尾应该正确"
    
    local result=$(utils_character_replace "hello world" "world" "universe")
    assert_equals "hello universe" "$result" "字符串替换应该正确"
    
    local result=$(utils_character_remove "hello world" "world")
    assert_equals "hello " "$result" "字符串删除应该正确"
    
    local result=$(utils_character_reverse "hello")
    assert_equals "olleh" "$result" "字符串反转应该正确"
}

# 测试: 大小写转换
test_utils_character_case_conversion() {
    local result=$(utils_character_to_upper "hello")
    assert_equals "HELLO" "$result" "转大写应该正确"
    
    local result=$(utils_character_to_lower "WORLD")
    assert_equals "world" "$result" "转小写应该正确"
    
    local result=$(utils_character_capitalize "hello world")
    assert_equals "Hello world" "$result" "首字母大写应该正确"
}

# 测试: 字符串清理函数
test_utils_character_cleaning() {
    local result=$(utils_character_trim "  hello world  ")
    assert_equals "hello world" "$result" "去除首尾空格应该正确"
    
    local result=$(utils_character_remove_spaces "hello world")
    assert_equals "helloworld" "$result" "去除所有空格应该正确"
    
    local result=$(utils_character_remove_newlines "hello\nworld")
    assert_equals "helloworld" "$result" "去除换行符应该正确"
    
    local result=$(utils_character_remove_tabs "hello\tworld")
    assert_equals "helloworld" "$result" "去除制表符应该正确"
    
    local result=$(utils_character_remove_whitespace "hello \t\nworld")
    assert_equals "helloworld" "$result" "去除所有空白字符应该正确"
}

# 测试: 去除重复字符
test_utils_character_remove_duplicates() {
    local result=$(utils_character_remove_duplicates "hello")
    assert_equals "helo" "$result" "去除重复字符应该正确"
    
    local result=$(utils_character_remove_duplicates "aabbcc" "a")
    assert_equals "abbcc" "$result" "去除指定重复字符应该正确"
}

# 测试: 字符串验证函数
test_utils_character_validation() {
    assert_true "长度检查应该通过" "utils_character_check_length 'hello' 3 10"
    assert_false "长度检查应该失败" "utils_character_check_length 'hi' 3 10"
    
    assert_true "空字符串检查应该正确" "utils_character_is_empty ''"
    assert_false "非空字符串检查应该正确" "utils_character_is_empty 'hello'"
    
    assert_true "纯字母检查应该正确" "utils_character_is_alpha_only 'hello'"
    assert_false "混合字符检查应该失败" "utils_character_is_alpha_only 'hello123'"
    
    assert_true "纯数字检查应该正确" "utils_character_is_digit_only '123'"
    assert_false "混合字符检查应该失败" "utils_character_is_digit_only '123abc'"
    
    assert_true "字母数字检查应该正确" "utils_character_is_alphanumeric_only 'hello123'"
    assert_false "包含符号检查应该失败" "utils_character_is_alphanumeric_only 'hello123!'"
}

# 测试: 正则表达式工具
test_utils_character_regex() {
    assert_true "正则匹配应该正确" "utils_character_regex_match 'hello123' '[0-9]+'"
    assert_false "正则不匹配应该正确" "utils_character_regex_match 'hello' '[0-9]+'"
    
    local result=$(utils_character_regex_extract 'hello123world' '[0-9]+')
    assert_equals "123" "$result" "正则提取应该正确"
}

# 测试: 字符统计
test_utils_character_count() {
    local result=$(utils_character_count "hello123")
    assert_equals "8" "$result" "总字符数应该正确"
    
    local result=$(utils_character_count "hello123" "letter")
    assert_equals "5" "$result" "字母数量应该正确"
    
    local result=$(utils_character_count "hello123" "digit")
    assert_equals "3" "$result" "数字数量应该正确"
}

# 测试: 编码检测和转换
test_utils_character_encoding() {
    local result=$(utils_character_detect_encoding "hello")
    assert_equals "ascii" "$result" "ASCII字符串应该被正确识别"
    
    local result=$(utils_character_detect_encoding "中文")
    # 注意：这个测试可能因环境而异，主要测试函数不报错
    assert_not_empty "$result" "编码检测应该返回结果"
    
    local result=$(utils_character_convert_encoding "hello" "utf-8" "utf-8")
    assert_equals "hello" "$result" "相同编码转换应该返回原字符串"
}

# 测试: 边界情况
test_utils_character_edge_cases() {
    # 空字符串测试
    local result=$(utils_character_substring "" 0 5)
    assert_equals "" "$result" "空字符串截取应该返回空字符串"
    
    local result=$(utils_character_replace "" "old" "new")
    assert_equals "" "$result" "空字符串替换应该返回空字符串"
    
    local result=$(utils_character_to_upper "")
    assert_equals "" "$result" "空字符串转大写应该返回空字符串"
    
    # 单字符测试
    local result=$(utils_character_reverse "a")
    assert_equals "a" "$result" "单字符反转应该返回原字符"
    
    local result=$(utils_character_capitalize "a")
    assert_equals "A" "$result" "单字符首字母大写应该正确"
}

# 测试: 特殊字符处理
test_utils_character_special_chars() {
    local result=$(utils_character_replace "hello@world.com" "@" "[at]")
    assert_equals "hello[at]world.com" "$result" "特殊字符替换应该正确"
    
    local result=$(utils_character_remove "hello!world!" "!")
    assert_equals "helloworld!" "$result" "特殊字符删除应该正确"
    
    local result=$(utils_character_count "hello!@#$%world" "symbol")
    assert_equals "5" "$result" "特殊字符统计应该正确"
}

###############################################################################
# 运行测试
###############################################################################

echo "========================================"
echo "测试套件: 字符串处理工具 (utils_character.sh)"
echo "========================================"

# 运行所有测试
run_test "字符类型检测" test_utils_character_get_type
run_test "字符类型判断函数" test_utils_character_type_checks
run_test "字符串格式化函数" test_utils_character_formatting
run_test "大小写转换" test_utils_character_case_conversion
run_test "字符串清理函数" test_utils_character_cleaning
run_test "去除重复字符" test_utils_character_remove_duplicates
run_test "字符串验证函数" test_utils_character_validation
run_test "正则表达式工具" test_utils_character_regex
run_test "字符统计" test_utils_character_count
run_test "编码检测和转换" test_utils_character_encoding
run_test "边界情况" test_utils_character_edge_cases
run_test "特殊字符处理" test_utils_character_special_chars

echo "----------------------------------------"
echo "测试套件 [字符串处理工具 (utils_character.sh)] 完成"
echo "----------------------------------------"
